# -*- coding: utf-8 -*-
# $Id: twoprogressbar.py 26 2009-02-15 04:09:09Z gkuznets $

# mpcheckup
# Copyright 2008, 2009 Grigoriy Kuznetsov (gkuznets@yandex.ru)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from PyQt4.QtGui import QWidget, QPainter,QBrush
from PyQt4.QtCore import QRect
from gui.colors import *

class TwoProgressBar(QWidget):
    def __init__(self, parent=None):
        QWidget.__init__(self, parent)
        self.__maximum = 100
        self.__minimum = 0
        self.__values = (0, 0)
        self.__colors = (checked_once_color, checked_twice_color, non_checked_color)
        
    def __checkValue(self, value):
        return self.__minimum <= value <= self.__maximum
    
    def setValues(self, pair):
        if self.__checkValue(pair[0]) and \
           self.__checkValue(pair[1]) and \
           pair[0] <= pair[1]:
            self.__values = pair
    
    def setValue(self, index, value):
        if self.__checkValue(value):
            if index == 0 and value <= self.__values[1]:
                self.__values = (value, self.__values[1])
            elif index == 1 and value >= self.__values[0]:
                self.__values = (self.__values[0], value)
    
    def values(self):
        return self.__values
    
    def paintEvent(self, event):
        QWidget.paintEvent(self, event)
        p = QPainter(self)
        # Рисуем 3
        p.setPen(non_checked_color)
        p.drawRoundedRect(QRect(0, 0, self.width()-1, self.height()-2), 1, 1)
        p.fillRect(QRect(1, 1, self.width()-2, self.height()-2), non_checked_color)
        # Рисуем 2
        if self.__values[1] > 0:
            p.setPen(checked_once_color)
            p.drawRoundedRect(QRect(0, 0, self.width()*(float(self.__values[1])/float(self.__maximum))-1, self.height()-2), 1, 1)
            p.fillRect(QRect(1, 1, self.width()*(float(self.__values[1])/float(self.__maximum))-2, self.height()-2), checked_once_color)
        # Рисуем 3
        if self.__values[0] > 0:
            p.setPen(checked_twice_color)
            p.drawRoundedRect(QRect(0, 0, self.width()*(float(self.__values[0])/float(self.__maximum))-1, self.height()-2), 1, 1)
            p.fillRect(QRect(1, 1, self.width()*(float(self.__values[0])/float(self.__maximum))-2, self.height()-2), checked_twice_color)
        p.end()